//
//  GLBadgeViewController.h
//  GLFormKitDemo
//
//  Created by 121 on 2018/3/28.
//  Copyright © 2018年 杭州吉利控股信息中心. All rights reserved.
//

#import "GLFormTextViewInputCell.h"
#import "GLPlaceholderTextView.h"
#import <Masonry/Masonry.h>
@interface GLFormTextViewInputCell()<UITextViewDelegate>

@end


@implementation GLFormTextViewInputCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.titleLab = [[UILabel alloc] init];
        self.titleLab.textColor = [UIColor grayColor];
        self.titleLab.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.titleLab];
        
        _textView = [[GLPlaceholderTextView alloc] init];
        _textView.font = [UIFont systemFontOfSize:16];
        _textView.delegate = self;
        _textView.placeholder = @"请输入";
        _textView.placeholderColor = [UIColor lightGrayColor];
        _textView.placeholderLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:_textView];
        
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(12);
            make.left.equalTo(self.contentView).offset(15);
        }];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLab).offset(-8);
            make.left.equalTo(self.titleLab.mas_right).offset(4);
            make.right.equalTo(self.contentView).offset(-15);
            make.bottom.equalTo(self.contentView).offset(-5);
        }];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)setLimitTitleWordNumber:(NSInteger)limitTitleWordNumber{
    if (_limitTitleWordNumber != limitTitleWordNumber) {
        _limitTitleWordNumber = limitTitleWordNumber;
        
        NSMutableString* widthStr  = [NSMutableString stringWithCapacity:0];
        for (NSInteger index = 0; index < _limitTitleWordNumber; index++) {
            [widthStr appendString:@"中"];
        }
        NSMutableDictionary *attrsDict = [NSMutableDictionary dictionary];
        attrsDict[NSFontAttributeName] = self.titleLab.font;
        
        CGFloat width = [widthStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 25) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrsDict context:nil].size.width;
        
        [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
        }];
    }
    
}

#pragma mark- UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString* newStr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    [self valueDidChanged:newStr];
    return YES;
}

/*
 * need override
 */

-(void)setRowValue:(id)value{
    _textView.text = value;
}
/*
 *need call
 */
//-(void)valueDidChanged:(id)newValue;
//-(void)clicked:(id)sender subTag:(NSString*)tag;

@end
